#include <string.h>
#include "methodpost.h"

void AddSpecialSymbols(char * param, char * newParam, char symbol, int append)
{
	size_t len = strlen(param);
	strcpy(newParam + 1, param);
	newParam[0] = symbol;
	if (append == 1)
	{
		newParam[len + 1] = '=';
		newParam[len + 2] = '\0';
	}
	else
	{
		newParam[len + 1] = '\0';
	}
}

char * GetValue(char * param, const char * const data, char * value)
{
	size_t currPos = 0;
	char * pos = NULL;
	char newParam[255];
	AddSpecialSymbols(param, newParam, '?', 1);
	if (!(pos = strstr(data, newParam)))
	{
		AddSpecialSymbols(param, newParam, '\n', 1);
		if (!(pos = strstr(data, newParam)))
		{
			return NULL;
		}
	}
	pos = pos + strlen(param) + 2;
	if ((*pos == '\0') || (*pos == '\n'))
	{
		return NULL;
	}
	while ((*pos != '\0') && (*pos != '\n'))
	{
		value[currPos] = *pos;
		currPos++;
		pos++;
	}
	value[currPos] = '\0';
	return value;
}

char * ToLower(char * str)
{
	int i;
	for (i = 0; str[i] != '\0'; ++i)
	{
		if ((str[i] >= 'A') && (str[i] <= 'Z'))
		{
			str[i] += 32;
		}
	}
	return str;
}